package com.lianzhihui.romeex.ui.me.fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.ExchangeRecordFragmentAdapter;
import com.lianzhihui.romeex.bean.ExchangeFindExchangeFlowBean;
import com.lianzhihui.romeex.mvp.presenter.ExchangeRecordPresnterImp;
import com.lianzhihui.romeex.mvp.view.FragmentExchangeView;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.lianzhihui.romeex.ui.me.CreditRecordActivity;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.trade.fragment
 * @ClassName: PledgeFramgent
 * @Description: 质押记录
 * @Author: zhanghong
 * @CreateDate: 2020/10/30 14:39
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/10/30 14:39
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class AddRecodeFragment extends BaseFragment implements FragmentExchangeView, OnLoadMoreListener {


    @BindView(R.id.myRecyclerView)
    ZFEmptyRecyclerView myRecyclerView;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    private ExchangeRecordPresnterImp exchangeRecordPresnterImp;
    private final int pageSize = 20;
    private ExchangeRecordFragmentAdapter exchangeRecordFragmentAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_recode;
    }

    @Override
    protected void initView() {
        myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        exchangeRecordFragmentAdapter = new ExchangeRecordFragmentAdapter(getActivity(), 1, new ArrayList<>());
        myRecyclerView.setAdapter(exchangeRecordFragmentAdapter);
        mySmartRefreshLayout.setOnLoadMoreListener(this);
        exchangeRecordPresnterImp = new ExchangeRecordPresnterImp(getActivity(), this);
    }


    @Override
    public void responseFindHomeSuccess(ExchangeFindExchangeFlowBean data) {
        if (data == null||data.getContent()==null) {
            return;
        }

        if (pageNum == 1) {
            myRecyclerView.showEmptyView(data.getContent().size(), pageNum);
            exchangeRecordFragmentAdapter.setNewData(data.getContent());
        } else {
            exchangeRecordFragmentAdapter.addData(data.getContent());
        }
        pageNum++;
        mySmartRefreshLayout.finishLoadMore();
        if (null != ((CreditRecordActivity) getActivity())) {
            ((CreditRecordActivity) getActivity()).ChildFragmentFinished();
        }
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        pageNum = 1;
        exchangeRecordPresnterImp.requestExchangeFindExchangeFlow(String.valueOf(pageNum), String.valueOf(pageSize), "1");
    }


    @Override
    public void setFailure() {
        mySmartRefreshLayout.finishLoadMore();
    }


    public void onRefresh() {
        pageNum = 1;
        exchangeRecordPresnterImp.requestExchangeFindExchangeFlow(String.valueOf(pageNum), String.valueOf(pageSize), "1");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mySmartRefreshLayout.finishLoadMore();
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        exchangeRecordPresnterImp.requestExchangeFindExchangeFlow(String.valueOf(pageNum), String.valueOf(pageSize), "1");
    }
}